package com.xnyzc.lhy.common.util;

import java.util.*;

/**
 * 抽奖算法工具
 */
public class LuckDrawUtil {

    public static String weightRandom(Map<String, Integer> map) {
        Set<String> keySet = map.keySet();
        List<String> weights = new ArrayList<>();
        for (Iterator<String> it = keySet.iterator(); it.hasNext(); ) {
            String weightStr = it.next();
            int weight = map.get(weightStr);
            for (int i = 0; i < weight; i++) {
                weights.add(weightStr);
            }
        }
        int idx = new Random().nextInt(weights.size());
        return weights.get(idx);

    }

    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>(16);
        //往map里添加的value值90,9,1代表着各自金额抽中的概率
        map.put("17589817777", 97);
        map.put("13350855307", 3);
        int i;
        List<String> l1=new ArrayList<>();
        List<String> l2=new ArrayList<String>(); List<String> l3=new ArrayList<String>();
        for (i=0; i < 100; i++) {
            String re=weightRandom(map);
            if ("17589817777".equals(re)){
                l1.add(re);
            }
            if ("13350855307".equals(re)){
                l2.add(re);
            }
            if("18581777797".equals(re)){
                l3.add(re);
            }
        }
        System.out.println("共随机 "+i+"次;\n17589817777 随机到的数量："+l1.size()+"\n"+
                "13350855307 随机到的数量："+l2.size()+"\n"+
                "18581777797 随机到的数量："+l3.size());
    }
}
